package service;

import dao.OrderDao;
import entity.Address;
import entity.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import relationEntity.CollectionProduct;
import relationEntity.OrderAddress;
import relationEntity.OrderProduct;
import relationEntity.UserAddress;
import util.Page;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by jifeng on 2016/12/15 0015.
 */
@Transactional
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    public OrderDao getOrderDao() {
        return orderDao;
    }

    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }
    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page OrderList(int currentPage,String paymentStatus,String sendStatus,String orderCode,String userId,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = orderDao.getOrderAllRowCount(paymentStatus,sendStatus,orderCode,userId);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Orders> list = orderDao.orderList(offset,paymentStatus,sendStatus,orderCode,userId,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }


    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page cancelOrderList(int currentPage,String paymentStatus,String sendStatus,String orderCode,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = orderDao.getCancelOrderAllRowCount(paymentStatus,sendStatus,orderCode);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Orders> list = orderDao.cancelOrderList(offset,paymentStatus,sendStatus,orderCode,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }


    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page OrderAddressList(int currentPage,String orderCode,String paymentStatus,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = orderDao.getOrderAddressAllRowCount(orderCode,paymentStatus);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<OrderAddress> list = orderDao.orderAddressList(offset,orderCode,paymentStatus,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public void deleteOrder(String ids){
        orderDao.deleteOrder(ids);
    }

    public OrderAddress orderDetails(int id){
       OrderAddress orderAddress =orderDao.orderDetails(id);
       return orderAddress;
    }

    public void updateOrder(Orders orders){
        orderDao.update(orders);
    }

    public Address findAddressById(int id){
        Address address =orderDao.findAddressById(id);
        return address;
    }

    public void updateAddress(Address address,String detailed){
        orderDao.updateAddress(address,detailed);
    }

    public void recoveryOrder(String ids){
        orderDao.recoveryOrder(ids);
    }

    public List<Address> findUserAddressByUserId(int id){
        return orderDao.findUserAddressByUserIds(id);
    }

    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page findUserAddressByUserId(int currentPage,int id,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = orderDao.getUserAddressAllRowCount(id);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Address> list = orderDao.findUserAddressByUserId(offset,id,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public Address findUserAddress(int id){
       return orderDao.findUserAddressById(id);
    }

    public void addAddress(Address address){
        orderDao.addAddress(address);
    }

    public void addOrder(Orders orders){
        orderDao.addOrder(orders);
    }

    public void updateOrder(String orderCode,int orderId){orderDao.updateOrder(orderCode,orderId);}

    public Orders findOrderById(int id){ return orderDao.findOrderById(id);}

    public List<OrderProduct> findOrdersListByUserId(int id){
        return orderDao.findOrdersListByUserId(id);
    }

    public List<CollectionProduct> findCollection(int id){
        return orderDao.findCollection(id);
    }
    public void updateAddresses(int userId,int addressId,String addressee,String addressPhone,String zipCode,String detailed){
        orderDao.updateAddresses(userId,addressId,addressee,addressPhone,zipCode,detailed);
    }
}
